﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;
using System.Windows;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Input;

namespace v1_SweetGeeks.View
{
    public static class Draw
    {
        private static System.OperatingSystem osInfo = System.Environment.OSVersion;
        public static Canvas myCanvas;
        public static Brush defualtBrush;
        public static double defualtOpacity;

        public static void Initialize(ref Canvas GuiCanvas, Brush Brushdefualt, double opacityDefualt)
        {
            myCanvas = GuiCanvas;
            defualtBrush = Brushdefualt;
            defualtOpacity = opacityDefualt;
        }

        public static double CanvasHeight(double percent)
        {
            return (myCanvas.ActualHeight / 100.0) * percent;
        }

        public static double CanvasWidth(double percent)
        {
            return (myCanvas.ActualWidth / 100.0) * percent;
        }

        public static Point GetMouse(IInputElement that)
        {
            Point position = Mouse.GetPosition(that);
            return position;
        }

        public static double CurveValue(double start, double current, double end, double time)
        {
            bool positiv = end > start;

            double distance = Math.Abs(end - start);
            double currentDis = positiv ? current - start : start - current;
            double percent = 1.0 - (currentDis / distance);
            double speed = (distance / time) != 0.0 ? (distance / time) / (1000.0 / 120.0) : speed = 1.0;

            if(distance - currentDis < 0.1)
            {
                current = end;
                return current;
            }

            if(percent.Equals(double.NaN))
                percent = 0.0;

            if(percent <= 0.0)
                percent = 0.01;

            if(percent >= 1.0)
                percent = 0.99;

            percent *= Math.PI;

            if(positiv)
            {
                current += Math.Sin(percent) * speed;
            }
            else
            {
                current -= Math.Sin(percent) * speed;
            }

            return current;
        }

        public static void DrawLinedRectangle(double x1, double y1, double x2, double y2, Brush colour, double opacity)
        {
            Line line = new Line();
            line.X1 = x1;
            line.X2 = x2;
            line.Y1 = y1;
            line.Y2 = y1;
            line.Stroke = colour;
            myCanvas.Children.Add(line);

            line = new Line();
            line.X1 = x2;
            line.X2 = x2;
            line.Y1 = y1;
            line.Y2 = y2;
            line.Stroke = colour;
            myCanvas.Children.Add(line);

            line = new Line();
            line.X1 = x1;
            line.X2 = x2;
            line.Y1 = y2;
            line.Y2 = y2;
            line.Stroke = colour;
            myCanvas.Children.Add(line);

            line = new Line();
            line.X1 = x1;
            line.X2 = x1;
            line.Y1 = y1;
            line.Y2 = y2;
            line.Stroke = colour;
            myCanvas.Children.Add(line);
        }

        public static void DrawLinedRectangle(double x1, double y1, double x2, double y2)
        {
            DrawLinedRectangle(x1, y1, x2, y2, defualtBrush, defualtOpacity);
        }

        public static void DrawImage(double x1, double y1, double x2, double y2, ref Image img)
        {
            Canvas.SetTop(img, y1);
            Canvas.SetLeft(img, x1);
            Canvas.SetRight(img, x2);
            Canvas.SetBottom(img, y2);
            img.Width = Math.Abs(x1 - x2);
            img.Height = Math.Abs(y1 - y2);
            myCanvas.Children.Add(img);
        }

        public static Rectangle DrawPixel(double x, double y, Brush colour, double opacity)
        {
            Rectangle rec = new Rectangle();
            Canvas.SetTop(rec, y);
            Canvas.SetLeft(rec, x);
            rec.Width = 1;
            rec.Height = 1;
            rec.Fill = colour;
            rec.Opacity = opacity;
            myCanvas.Children.Add(rec);
            return rec;
        }

        public static Rectangle DrawPixel(double x, double y)
        {
            return DrawPixel(x, y, defualtBrush, defualtOpacity);
        }

        public static Rectangle DrawRectangle(double x1, double y1, double x2, double y2, Brush colour, double opacity)
        {
            Rectangle rec = new Rectangle();
            Canvas.SetTop(rec, y1);
            Canvas.SetLeft(rec, x1);
            Canvas.SetRight(rec, x2);
            Canvas.SetBottom(rec, y2);
            rec.Width = Math.Abs(x1 - x2);
            rec.Height = Math.Abs(y1 - y2);
            rec.Fill = colour;
            rec.Opacity = opacity;
            myCanvas.Children.Add(rec);
            return rec;
        }

        public static Rectangle DrawRectangle(double x1, double y1, double x2, double y2)
        {
            return DrawRectangle(x1, y1, x2, y2, defualtBrush, defualtOpacity);
        }

        public static Line DrawLine(double x1, double y1, double x2, double y2, int Thickness, Brush colour, double opacity)
        {
            Line line = new Line();

            line.Stroke = colour;

            line.X1 = x1;
            line.Y1 = y1;
            line.X2 = x2;
            line.Y2 = y2;

            line.StrokeThickness = Thickness;
            line.Opacity = opacity;

            myCanvas.Children.Add(line);
            return line;
        }

        public static Line DrawLine(double x1, double y1, double x2, double y2)
        {
            return DrawLine(x1, y1, x2, y2, 1, defualtBrush, defualtOpacity);
        }

        public static TextBlock DrawText(double x, double y, double fontSize, string text, Brush colour, double opacity)
        {
            TextBlock txt = new TextBlock();
            Canvas.SetTop(txt, y);
            Canvas.SetLeft(txt, x);
            txt.Text = text;
            txt.FontSize = fontSize;
            if(osInfo.Version.Major.ToString()+osInfo.Version.Minor.ToString() == "61")
            {
                txt.FontFamily = new FontFamily("Moire ExtraBold");
            }
            else
            {
               txt.FontFamily = new FontFamily("Aharoni"); 
            }
            txt.Foreground = colour;
            txt.Opacity = opacity;
            myCanvas.Children.Add(txt);
            return txt;
        }

        public static TextBlock DrawText(double x, double y, double fontSize, string text)
        {
            return DrawText(x, y, fontSize, text, defualtBrush, defualtOpacity);
        }

        public static TextBlock DrawText(double x, double y, double fontSize, string text, Brush colour, double opacity, out Size measurement)
        {
            TextBlock txt = new TextBlock();
            Canvas.SetTop(txt, y);
            Canvas.SetLeft(txt, x);
            txt.Text = text;
            txt.FontSize = fontSize;
            if(osInfo.Version.Major.ToString()+osInfo.Version.Minor.ToString() == "61")
            {
                txt.FontFamily = new FontFamily("Moire ExtraBold");
            }
            else
            {
               txt.FontFamily = new FontFamily("Aharoni"); 
            }
            txt.Foreground = colour;
            txt.Opacity = opacity;
            myCanvas.Children.Add(txt);
            measurement = MeasureTextSize(text,txt.FontFamily,txt.FontStyle,txt.FontWeight,txt.FontStretch,fontSize);
            return txt;
        }

        public static TextBlock DrawCenterText(double x, double y, double fontSize, string text, Brush colour, double opacity)
        {
            Size size = GetTextSize(text, fontSize);

            return DrawText(x - (size.Width * 0.5), y, fontSize, text, colour, opacity);
        }

        public static Size GetTextSize(string text, double fontSize)
        {
            TextBlock txt = new TextBlock();
            txt.Text = text;
            txt.FontSize = fontSize;
            txt.Opacity = 0.0;
            return MeasureTextSize(text, txt.FontFamily, txt.FontStyle, txt.FontWeight, txt.FontStretch, fontSize);
        }

        private static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
        {
            FormattedText ft = new FormattedText(text,
                                                 CultureInfo.CurrentCulture,
                                                 FlowDirection.LeftToRight,
                                                 new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                                                 fontSize,
                                                 Brushes.Black);
            return new Size(ft.Width, ft.Height*0.95);
        }
    }
}
